/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.helloworld;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class HelloWorld implements ApplicationListener {
	SpriteBatch spriteBatch;
	Texture texture;
	Pixmap bolinha;
	Vector2 bolinhaPosition = new Vector2(100, 100);
	Vector2 bolinhaDirection = new Vector2(1, 1);

	@Override
	public void create () {
		Pixmap.setBlending(Pixmap.Blending.None);
		bolinha = new Pixmap(Gdx.files.internal("data/ball.png"));
		bolinha.setColor(Color.RED);
		Pixmap pixmap = new Pixmap(256, 256, Pixmap.Format.RGBA8888);
		texture = new Texture(Gdx.files.internal("data/ball.png"));
		spriteBatch = new SpriteBatch();
	}

	@Override
	public void render () {
		int centerX = Gdx.graphics.getWidth() / 2;
		int centerY = Gdx.graphics.getHeight() / 2;

		Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// more fun but confusing :)
		// textPosition.add(textDirection.tmp().mul(Gdx.graphics.getDeltaTime()).mul(60));
		bolinhaPosition.x += bolinhaDirection.x * Gdx.graphics.getDeltaTime() * 90;
		bolinhaPosition.y += bolinhaDirection.y * Gdx.graphics.getDeltaTime() * 90;

		if (bolinhaPosition.x < 0) {
			bolinhaDirection.x = -bolinhaDirection.x;
			bolinhaPosition.x = 0;
		}
		if (bolinhaPosition.x+bolinha.getWidth() > Gdx.graphics.getWidth()) {
			bolinhaDirection.x = -bolinhaDirection.x;
			bolinhaPosition.x = Gdx.graphics.getWidth()-bolinha.getWidth();
		}
		if (bolinhaPosition.y < 0) {
			bolinhaDirection.y = -bolinhaDirection.y;
			bolinhaPosition.y = 0;
		}
		if (bolinhaPosition.y+bolinha.getHeight() > Gdx.graphics.getHeight()) {
			bolinhaDirection.y = -bolinhaDirection.y;
			bolinhaPosition.y = Gdx.graphics.getHeight()-bolinha.getHeight();
		}

		spriteBatch.begin();
		spriteBatch.setColor(Color.WHITE);
//		texture.draw(bolinha);
		texture.bind();
		spriteBatch.draw(texture,bolinhaPosition.x,bolinhaPosition.y);
		spriteBatch.end();
	}

	@Override
	public void resize (int width, int height) {
//		spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
		bolinhaPosition.set(0, 0);
	}

	@Override
	public void pause () {

	}

	@Override
	public void resume () {

	}

	@Override
	public void dispose () {

	}

}
